<template>
  <div class="app-container">
    
    <LastUpdate :time="time" :onClickUpdate="handleClickUpdate"/>

    <el-card >
      <el-table v-loading="tableLoading" :data="dataList" stripe border >
        <el-table-column label="参数" align="left" prop="param" sortable >
          <template slot-scope="{row}">
            <el-tag :color="textInfo[row.param].color">{{ row.param }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="描述" align="left" prop="text"  >
          <template slot-scope="{row}">
            {{ textInfo[row.param].text }}
          </template>
        </el-table-column>
        <el-table-column label="当前值" align="left" :show-overflow-tooltip="true" >
          <template slot-scope="{row}">
            {{ row.value }} {{ textInfo[row.param].unit }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
  import {httpGet} from '@/api';

  export default {
    data() {
      return {
        time: '',
        tableLoading: true,
        dataList: [],
        // 列表通过 textInfo 渲染出颜色、描述和单位
        textInfo: {
          'tomcat.sessions.created': { color: '#e8f4ff', text: 'tomcat 已创建 session 数', unit: '个' },
          'tomcat.sessions.expired': { color: '#e8f4ff', text: 'tomcat 已过期 session 数', unit: '个' },
          'tomcat.sessions.active.current': { color: '#e8f4ff', text: 'tomcat 当前活跃 session 数', unit: '个' },
          'tomcat.sessions.active.max': { color: '#e8f4ff', text: 'tomcat 活跃 session 数峰值', unit: '个' },
          'tomcat.sessions.rejected': { color: '#e8f4ff', text: '超过session 最大配置后，拒绝的 session 个数', unit: '个' },

          'tomcat.global.sent': { color: '#f5cfd5', text: '发送的字节数', unit: 'bytes' },
          'tomcat.global.request.max': { color: '#f5cfd5', text: 'request 请求最长耗时', unit: '秒' },
          'tomcat.global.request.count': { color: '#f5cfd5', text: '全局 request 请求次数', unit: '次' },
          'tomcat.global.request.totalTime': { color: '#f5cfd5', text: '全局 request 请求总耗时', unit: '秒' },

          'tomcat.servlet.request.max': { color: '#ececbd', text: 'servlet 请求最长耗时', unit: '秒' },
          'tomcat.servlet.request.count': { color: '#ececbd', text: 'servlet 总请求次数', unit: '次' },
          'tomcat.servlet.request.totalTime': { color: '#ececbd', text: 'servlet 请求总耗时', unit: '秒' },

          'tomcat.threads.current': { color: '#e6d8ef', text: 'tomcat 当前线程数（包括守护线程）', unit: '个' },
          'tomcat.threads.config.max': { color: '#e6d8ef', text: 'tomcat 配置的线程最大数', unit: '个' }
        },
        // 当一条记录中需要取出多条数据的时候需要配置该字段
        moreInfo: {
          'tomcat.global.request': ['.count', '.totalTime'],
          'tomcat.servlet.request': ['.count', '.totalTime']
        }
      }
    },
    mounted() {
      this.loadTomcatInfo()
    },
    methods: {

      handleClickUpdate() {
        this.loadTomcatInfo()
      },

      loadTomcatInfo() {
        this.tableLoading = true
        this.time = this.$formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
        Promise.all([
          httpGet('actuator/metrics/tomcat.sessions.created'),
          httpGet('actuator/metrics/tomcat.sessions.expired'),
          httpGet('actuator/metrics/tomcat.sessions.active.current'),
          httpGet('actuator/metrics/tomcat.sessions.active.max'),
          httpGet('actuator/metrics/tomcat.sessions.rejected'),
          // 2.3.5.RELEASE 无此API
          // httpGet('actuator/metrics/tomcat.global.sent'),
          // httpGet('actuator/metrics/tomcat.global.request.max'),
          // httpGet('actuator/metrics/tomcat.global.request'),
          // httpGet('actuator/metrics/tomcat.threads.current'),
          // httpGet('actuator/metrics/tomcat.threads.config.max')
          // 2.1.3.RELEASE 无此API
          //httpGet('actuator/metrics/tomcat.servlet.request'),
          // httpGet('actuator/metrics/tomcat.servlet.request.max'),
        ]).then((res) => {
          let tomcatInfo = []
          res.forEach((value, id) => {
            let more = this.moreInfo[value.name]
            if (!(more instanceof Array)) {
              more = ['']
            }
            more.forEach((item, idx) => {
              let param = value.name + item
              tomcatInfo.push({
                id: param + id, param,
                text: 'false value',
                value: value.measurements[idx].value
              })
            })
          })
          this.dataList = tomcatInfo
        }).catch((e) => {
          console.error(e)
          this.$message.error('获取Tomcat信息失败')
        }).finally(() => {
          this.tableLoading = false
        })
      }
    }
  }
</script>
<style></style>
